const { isAndroid, isIOS, versionCompare, getAppVersion, isApp } = require('@jmfe/jm-common')

export const isJDAndroid = isAndroid()
export const isJDIOS = isIOS()
export const isJDAPP = isApp('jd')
export const jdVersionGTA916 = isApp('jd') && versionCompare(getAppVersion('jd'), '9.1.6') >= 0
const navData = {
	canPull: '0',
	supportTran: '1',
	tranParams: {
		hideNavi: '1',
		whiteImg: '',
		blackImg: '',
		backgroundColor: '#ffffff',
		naviMenuType: 'bb',
	},
	callBackName: 'configNaviCB', // 设置状态栏的结果回调
}

// WK环境识别（环境识别）
function isWKWebView() {
	const ua = navigator.userAgent
	return !!(!!ua.match(/supportJDSHWK/i) || window._is_jdsh_wkwebview == 1) //    true表示是ios商城，false表示是android商城
}

export const customNavigation = () => {
	const isWK = isWKWebView()
	return new Promise((resolve, reject) => {
		try {
			if (isJDAPP) {
				// 判断京东app版本大于916
				const jdAppGTA916 = versionCompare(getAppVersion('jd'), '9.1.6') >= 0
				if (jdAppGTA916) {
					navData.supportTran = '1'
					navData.tranParams.hideNavi = '1'
					navData.tranParams.naviMenuType = 'bb'
				} else {
					navData.supportTran = '0'
					navData.tranParams.hideNavi = '0'
					navData.tranParams.naviMenuType = 'bb'
				}
				console.log(navData)
				if (isWK) {
					// WKWebView
					window.webkit.messageHandlers.MobileNavi.postMessage({
						method: 'configNavigationBar',
						params: JSON.stringify(navData),
						// 设置状态栏的结果回调
					})
				} else {
					// UIWebView和Android
					window.MobileNavi &&
						window.MobileNavi.configNavigationBar(JSON.stringify(navData))
				}
				window.configNaviCB = (info) => {
					resolve(JSON.parse(info))
				}
			}
		} catch (e) {
			reject(e)
		}
	})
}

export const getNavigationHeight = () => {
	return new Promise((resolve, reject) => {
		try {
			if (isJDAPP) {
				if (isJDAndroid) {
					window.MobileNavi.getActualNaviStatusHeight('getStatusHeightcallback')
				}
				if (isJDIOS) {
					window.webkit.messageHandlers.JDAppUnite.postMessage({
						method: 'callRouterModuleWithParams',
						params: {
							routerURL: 'router://JDWebViewBusinessModule/getActualNaviStatusHeight', // required,必选参数
							callBackName: 'getStatusHeightcallback',
						},
					})
				}
				window.getStatusHeightcallback = (info) => {
					if (isJDAndroid) {
						resolve(JSON.parse(info))
					}
					if (isJDIOS) {
						const { data } = JSON.parse(info)
						resolve(data)
					}
				}
			}
		} catch (error) {
			reject(error)
		}
	})
}
